﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Redis;
using Microsoft.Extensions.Configuration;
using Microsoft.Net.Http;
using Newtonsoft.Json;
using ShopDAL.Models;

namespace NT40Redis.Controllers
{
    public class ProductController : Controller
    {
        RedisCache redis;
        IConfiguration config;
        public ProductController(IConfiguration configuration,RedisCache redisCache)
        {
            redis = redisCache;
            config = configuration;
        }
        public async Task<IActionResult> Index()
        {
            String proJson = redis.GetString("prods");
            if (string.IsNullOrEmpty(proJson))
            {
                HttpClient httpClient = new HttpClient();
                proJson = await httpClient.GetStringAsync(config.GetSection("APIUrl").Value);
                redis.SetString("prods", proJson);
            }
            IEnumerable<Products> prodlist = JsonConvert.DeserializeObject<IEnumerable<Products>>(proJson);
          
            return View(prodlist);
        }
    }
}